//
//  LiveOrderDetailVC.m
//  WuyouTang
//
//  Created by ybRRR on 2023/7/13.
//  Copyright © 2023 cat. All rights reserved.
//

#import "LiveOrderDetailVC.h"
#import "UpHotCell.h"
#import <YCMenuView.h>
#import "addHotVideoVC.h"
#import "UpHotView.h"
#import "YBLiveVC.h"
#import "YBVipVC.h"
typedef NS_ENUM(NSInteger,YBSelType){
    TypeSel_Live,
    TypeSel_Video,
};

@interface LiveOrderDetailVC ()<UITableViewDelegate, UITableViewDataSource,upHotCellDelegate>
{
    UIButton *selectBtn;
    
    NSString *typeStr;
    int pageIndex;
}
@property (nonatomic, strong)UITableView *listTable;
@property(nonatomic,strong)UIView *tabHeaderView;
@property(nonatomic,strong)NSArray *selectArr;
@property(nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UpHotView *hotView;

@end

@implementation LiveOrderDetailVC

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    self.view.backgroundColor = UIColor.redColor;
//    self.naviView.hidden = YES;
//}

- (void)viewWillAppear:(BOOL)animated{
    [UIApplication sharedApplication].statusBarStyle =  UIStatusBarStyleDefault;
}
- (void)viewWillDisappear:(BOOL)animated{
    [UIApplication sharedApplication].statusBarStyle =  UIStatusBarStyleLightContent;
}
-(void)getOrderList{
    NSDictionary *dic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken],@"status":typeStr,@"p":@(pageIndex)};
    [WYNetworking postWithUrl:@"Popular.getHotLiveOrders" Dic:dic Suc:^(int code, id info, NSString *msg) {
        [_listTable.mj_header endRefreshing];
        [_listTable.mj_footer endRefreshing];
        if (code == 0) {
            NSArray *listarr = info;
            if (pageIndex == 1) {
                [_dataArr removeAllObjects];
            }
            [_dataArr addObjectsFromArray:listarr];
            [_listTable reloadData];

            if (_dataArr.count > 0){
                [PublicView hiddenTextNoData:_listTable];
            } else{
                [PublicView showTextNoData:_listTable text1:@"" text2:YZMsg(@"暂无数据") centerY:0.8];
            }
        }
        } Fail:^(id fail) {
            [_listTable.mj_header endRefreshing];
            [_listTable.mj_footer endRefreshing];

        }];
}
-(void)creatNavi {
//    UIView *navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 64+statusbarHeight)];
//    navi.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:navi];
//
//    UIButton *left_btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    left_btn.frame = CGRectMake(10, 22+statusbarHeight, 40, 40);
//    left_btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
//    [left_btn setImage:[UIImage imageNamed:@"pubBlack_back"] forState:0];
//    [left_btn addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
//    [navi addSubview:left_btn];
//
//    UILabel *midLabel = [[UILabel alloc]initWithFrame:CGRectMake(_window_width/2-80, 22+statusbarHeight, 160, 40)];
//    midLabel.textAlignment = NSTextAlignmentCenter;
//    midLabel.textColor = RGB_COLOR(@"#323232", 1);
//    midLabel.font = [UIFont boldSystemFontOfSize:17];
//    midLabel.text = YZMsg(@"明细");
//    [navi addSubview:midLabel];
    
}
- (void)clickLeftBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviView.hidden = YES;
    typeStr = @"0";
    pageIndex = 1;
    _dataArr = [NSMutableArray array];
//    [self creatNavi];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor =RGB_COLOR(@"#fafafa", 1);
    [self.view addSubview:self.listTable];
    [self getOrderList];
}
-(UITableView *)listTable {
    if (!_listTable) {
        //5个像素间隔
        _listTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height-64-statusbarHeight-ShowDiff) style:UITableViewStylePlain];
        _listTable.delegate = self;
        _listTable.dataSource = self;
        _listTable.separatorStyle = UITableViewCellStyleDefault;
        _listTable.backgroundColor = RGB_COLOR(@"#fafafa", 1);
        _listTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            pageIndex = 1;
            [self getOrderList];

        }];
        _listTable.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
            pageIndex +=1;
            [self getOrderList];
        }];
    }
    return _listTable;
}
-(UIView *)tabHeaderView {
    if (!_tabHeaderView) {
        _tabHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 50)];
        _tabHeaderView.backgroundColor = RGB_COLOR(@"#fafafa", 1);
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 50)];
        title.font = SYS_Font(15);
        title.text = YZMsg(@"我的直播订单");
        title.textColor = [UIColor blackColor];
        [_tabHeaderView addSubview:title];
        
        selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        selectBtn.frame = CGRectMake(_window_width-90, 0, 75, 50);
        [selectBtn setTitle:YZMsg(@"未完成") forState:0];
        [selectBtn setImage:[UIImage imageNamed:@"hot_down"] forState:0];
        [selectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - selectBtn.imageView.image.size.width, 0, selectBtn.imageView.image.size.width)];
        [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, selectBtn.titleLabel.bounds.size.width, 0, -selectBtn.titleLabel.bounds.size.width)];

        selectBtn.titleLabel.font = SYS_Font(15);
        [selectBtn setTitleColor:GrayText forState:0];
        selectBtn.contentHorizontalAlignment = NSTextAlignmentRight;
        [selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_tabHeaderView addSubview:selectBtn];
    }
    return _tabHeaderView;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.tabHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UpHotCell *cell = [UpHotCell cellWithTab:tableView andIndexPath:indexPath];
    cell.delegate = self;
    cell.livedataDic = _dataArr[indexPath.row];
    cell.statuStr =typeStr;
    cell.gdy_tfsjL.text = YZMsg(@"曝光量");
    cell.gdy_playNumL.text = YZMsg(@"预计进场人数");
    cell.gdy_RealTimeL.text = YZMsg(@"直播时间");
    cell.gdy_realPlayNUmL.text = YZMsg(@"实际曝光量");

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)selectBtnClick:(UIButton *)sender{
    // 创建YCMenuAction
    YCMenuAction *action = [YCMenuAction actionWithTitle:YZMsg(@"已完成") image:nil handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        [selectBtn setTitle:YZMsg(action.title) forState:0];
        typeStr = @"1";
        pageIndex = 1;
        [self getOrderList];

    }];
    YCMenuAction *action1 = [YCMenuAction actionWithTitle:YZMsg(@"未完成") image:nil handler:^(YCMenuAction *action) {
        NSLog(@"点击了%@",action.title);
        [selectBtn setTitle:YZMsg(action.title) forState:0];
        typeStr = @"0";
        pageIndex = 1;
        [self getOrderList];

    }];
    // 按钮（YCMenuAction）的集合
    self.selectArr = @[action,action1];

    // 创建YCMenuView(根据关联点或者关联视图)
    YCMenuView *view = [YCMenuView menuWithActions:self.selectArr width:120 relyonView:sender];
    
    // 自定义设置
    view.menuColor = [UIColor whiteColor];
    view.separatorColor = [UIColor clearColor];
    view.maxDisplayCount = 5;  // 最大展示数量（其他的需要滚动才能看到）
    view.offset = 0; // 关联点和弹出视图的偏移距离
    view.textColor = [UIColor grayColor];
    view.textFont = [UIFont boldSystemFontOfSize:14];
    view.menuCellHeight = 44;
    view.dismissOnselected = YES;
    view.dismissOnTouchOutside = YES;
    
    // 显示
    [view show];

}
-(void)clickReUpliveOrder
{
    
    NSString *liveLimit = minstr([YBPower getLiveStatus]);
    NSString *liveMsg = minstr([YBPower getLiveMsg]);
    if ([liveLimit isEqual:@"1"]) {
        [MBProgressHUD showMessage:@""];
        [WYNetworking postWithUrl:@"Live.getLiveParameters" Dic:@{} Suc:^(int code, id info, NSString *msg) {
            [MBProgressHUD hideHUD];
            if (code == 0) {
                NSDictionary *infoDic = [info firstObject];
                YBLiveVC *liveVC = [[YBLiveVC alloc]init];
                liveVC.pushSettingDic = [infoDic valueForKey:@"ios"];
                
                liveVC.has_hotliveorder = minstr([infoDic valueForKey:@"has_hotliveorder"]);
                liveVC.hotliveorderDic =[infoDic valueForKey:@"hotliveorder"];
                // 是否禁止开播
                BOOL isBan = minstr([infoDic valueForKey:@"liveban_score"]);
                [YBPower saveBanLiveStatus:isBan];
                NSString *banStr = minstr([infoDic valueForKey:@"liveban_msg"]);
                [YBPower saveBanLiveMsg:banStr];
                
                [[YBBaseAppDelegate sharedAppDelegate]pushViewController:liveVC animated:YES];
            }else {
                [MBProgressHUD showPop:msg];
            }
        } Fail:^(id fail) {
            [MBProgressHUD hideHUD];
        }];
    }else {
        [self showAlert:TypeSel_Live andAlertMsg:liveMsg];
        //[MBProgressHUD showPop:liveMsg];
    }

    
//    YBWeakSelf;
//    _hotView = [[UpHotView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)];
//    _hotView.closeEvent = ^(NSString *status, NSString *orderid) {
//        if([status isEqual:@"确定"]){
////                uphotOrderId = orderid;
//        }
//        [weakSelf.hotView removeFromSuperview];
//        weakSelf.hotView = nil;
//
//    };
//    [self.view addSubview:_hotView];

}
-(void)showAlert:(YBSelType)selType andAlertMsg:(NSString *)alertMsg; {
   
    NSDictionary *contentDic = @{@"title":@"",
                                @"msg":alertMsg,
                                @"left":YZMsg(@"取消"),
                                @"right":(selType == TypeSel_Live)?YZMsg(@"权限说明"):YZMsg(@"开通会员")};
    [YBAlertView showAlertView:contentDic complete:^(int eventType) {
        if (eventType == 1) {
            NSString *url = [NSString stringWithFormat:@"%@/appapi/liveauthority/index?uid=%@&token=%@",h5url,[Config getOwnID],[Config getOwnToken]];
            
            if (selType == TypeSel_Video) {
                //url = [NSString stringWithFormat:@"%@/Appapi/Auth/index?uid=%@&token=%@",h5url,[Config getOwnID],[Config getOwnToken]];;
                YBVipVC *vipVC = [[YBVipVC alloc]init];
                [[YBBaseAppDelegate sharedAppDelegate]pushViewController:vipVC animated:YES];
            }else {
                PubH5 *h5VC = [[PubH5 alloc]init];
                h5VC.url = url;
                [[YBBaseAppDelegate sharedAppDelegate]pushViewController:h5VC animated:YES];
            }
        }
    }];
}

-(void)reUpOrderClick:(NSDictionary *)dic
{
    [MBProgressHUD showMessage:@""];
    [WYNetworking postWithUrl:@"Video.getVideo" Dic:@{@"videoid":minstr([dic valueForKey:@"videoid"]),@"mobileid":[PublicObj getDeviceUUID]} Suc:^(int code, id info, NSString *msg) {
        [MBProgressHUD hideHUD];
        if (code == 0) {
            NSDictionary *infoDic = [info firstObject];
            addHotVideoVC *hot = [[addHotVideoVC alloc]init];
            hot.videoInfo = infoDic;
            [[YBBaseAppDelegate sharedAppDelegate]pushViewController:hot animated:YES];

        }else {
            [MBProgressHUD showPop:msg];
        }
    } Fail:^(id fail) {
        [MBProgressHUD hideHUD];
    }];

    
}


@end
